package com.castle.fortress.admin.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsArticleSubjectDto;
import com.castle.fortress.admin.cms.entity.CmsArticleSubjectEntity;
import com.castle.fortress.admin.cms.mapper.CmsArticleSubjectMapper;
import com.castle.fortress.admin.cms.service.CmsArticleSubjectService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * cms内容标签管理表 服务实现类
 *
 * @author castle
 * @since 2022-03-18
 */
@Service
public class CmsArticleSubjectServiceImpl extends ServiceImpl<CmsArticleSubjectMapper, CmsArticleSubjectEntity> implements CmsArticleSubjectService {
    /**
    * 获取查询条件
    * @param cmsArticleSubjectDto
    * @return
    */
    public QueryWrapper<CmsArticleSubjectEntity> getWrapper(CmsArticleSubjectDto cmsArticleSubjectDto){
        QueryWrapper<CmsArticleSubjectEntity> wrapper= new QueryWrapper();
        if(cmsArticleSubjectDto != null){
            CmsArticleSubjectEntity cmsArticleSubjectEntity = ConvertUtil.transformObj(cmsArticleSubjectDto,CmsArticleSubjectEntity.class);
            wrapper.eq(cmsArticleSubjectEntity.getId() != null,"id",cmsArticleSubjectEntity.getId());
            wrapper.eq(cmsArticleSubjectEntity.getArticleId() != null,"article_id",cmsArticleSubjectEntity.getArticleId());
            wrapper.eq(cmsArticleSubjectEntity.getSubjectId() != null,"subject_id",cmsArticleSubjectEntity.getSubjectId());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsArticleSubjectDto> pageCmsArticleSubject(Page<CmsArticleSubjectDto> page, CmsArticleSubjectDto cmsArticleSubjectDto) {
        QueryWrapper<CmsArticleSubjectEntity> wrapper = getWrapper(cmsArticleSubjectDto);
        Page<CmsArticleSubjectEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsArticleSubjectEntity> cmsArticleSubjectPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsArticleSubjectDto> pageDto = new Page(cmsArticleSubjectPage.getCurrent(), cmsArticleSubjectPage.getSize(),cmsArticleSubjectPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsArticleSubjectPage.getRecords(),CmsArticleSubjectDto.class));
        return pageDto;
    }


    @Override
    public List<CmsArticleSubjectDto> listCmsArticleSubject(CmsArticleSubjectDto cmsArticleSubjectDto){
        QueryWrapper<CmsArticleSubjectEntity> wrapper = getWrapper(cmsArticleSubjectDto);
        List<CmsArticleSubjectEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsArticleSubjectDto.class);
    }
    @Async
    @Override
    public void updateArtiSubBatch(List<Long> articleids, List<Long> special) {
        if(articleids!=null && !articleids.isEmpty()){
            QueryWrapper<CmsArticleSubjectEntity> wrapper = new QueryWrapper<>();
            wrapper.in("article_id",articleids);
            baseMapper.delete(wrapper);
            if(special !=null && !special.isEmpty()){
                List<CmsArticleSubjectEntity> list = new ArrayList<>();
                for(Long article:articleids){
                    for(Long spid:special){
                        CmsArticleSubjectEntity entity = new CmsArticleSubjectEntity();
                        entity.setArticleId(article);
                        entity.setSubjectId(spid);
                        list.add(entity);
                    }
                }
                saveBatch(list);
            }
        }
    }
}

